// pages/register/bindphone2/bindphone2.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    generatedCode: '',
    countdown: 0,
    countdownText: '',
    isButtonDisabled: false,
    timer: null,
    userInputCode: "",
    beforephone: "",
    id: ""
  },
  generateCode: function () {
    // 生成6位随机验证码
    return Math.floor(100000 + Math.random() * 900000).toString();
  },

  startCountdown: function () {
    const code = this.generateCode(); // 生成验证码
    this.setData({
      generatedCode: code, // 存储生成的验证码
    });

    if (this.data.phone === "") {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
      });
    } else if (!this.data.phone.match(/^\d{11}$/)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
      });
    } else {
      const that = this;
      wx.request({
        url: 'http://localhost:8080/user/select',
        data: {
          phone: this.data.phone
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: "GET",
        success(resp) {
          let r = resp.data;
          // console.log(r);
          if (r.data === -1) {
            wx.showToast({
              title: '手机号已被注册',
              icon: 'none',
            });
          } else {
            if (that.data.timer) {
              clearInterval(that.data.timer);
            }
            that.setData({
              countdown: 60,
              countdownText: '剩余60秒',
              isButtonDisabled: true
            });
            that.data.timer = setInterval(() => {
              let newCountdown = that.data.countdown - 1;
              if (newCountdown <= 0) {
                clearInterval(that.data.timer);
                that.setData({
                  countdownText: '',
                  countdown: 0,
                  isButtonDisabled: false
                });
              } else {
                that.setData({
                  countdownText: `剩余${newCountdown}秒`,
                  countdown: newCountdown
                });
              }
            }, 1000);
            wx.showModal({
              title: '提示',
              content: `您的验证码是: ${code}`,
              showCancel: false,
              confirmText: '确定',
            });
          }
        },
        fail(err) {
          console.error('请求失败', err);
          wx.showToast({
            title: '请求失败，请稍后再试',
            icon: 'none',
          });
        }
      });
    }
  },

  ToNext() {
    if (this.data.phone === "") {
      wx.showToast({
        title: '请输入手机号，通过手机号获取验证码',
        icon: 'none',
      });
    } else if (this.data.userInputCode === "") {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none',
      });
    } else if (this.data.userInputCode !== this.data.generatedCode) {
      wx.showToast({
        title: '验证码不正确',
        icon: 'none',
      });
    } else {
      wx.request({
        url: 'http://localhost:8080/replace/select2',
        method: "GET",
        data: {
          phone: this.data.beforephone
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: (resp) => {
          let r = resp.data.data;
          console.log(r);
          if (r && (r.role === 3)) {
            this.setData({
              id: r.id
            });
            console.log(this.data.id);
            wx.request({
              url: 'http://localhost:8080/replace/update',
              method: "POST",
              data: {
                id: this.data.id,
                phone: this.data.phone
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: (resp) => {
                let r = resp.data;
                console.log(r);
                wx.navigateTo({
                  url: '/pages/register/bindphone3/bindphone3',
                })
              }
            })
          }
        },
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options && options.constphone) {
      this.setData({
        beforephone: options.constphone
      });
    } else {
      console.warn("No phone parameter provided or options is undefined.");
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})